﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Shopping_Cart
/// </summary>
/// 


namespace Bestlife.ERP.BusinessRules
{

    #region Create Cart ItemList

    public class Shopping_Cart
    {
    
    List<Entities.Cart_Item> items;
    
    List<Entities.Return_to_R> rtnrcptList=new List<Entities.Return_to_R>();
    List<Entities.Tocheck_To_cart> check_ToList = new List<Entities.Tocheck_To_cart>();

    public List<Entities.Cart_Item> Items
    {
        get { return items; }
        set { items = value; }
    }

    #endregion

    #region Shopping CartList

    public Shopping_Cart()
	{
        items = new List<Entities.Cart_Item>();
	}

    #endregion

    #region Find Correct Index

    public int Findindex(string Itemid, string code)
    {
        int index = -1;
        foreach (Entities.Cart_Item c in items)
        {
            index++;
            if (c.ID.Equals(Itemid) && c.Code.Equals(code))
            {

                return index;
            }

        }

        return -1;

    }

    #endregion 

    #region Insert Shopping Cart Item

    public void insertItems(string id, string code, string name, decimal pri,decimal r_price, decimal pointv, decimal busi_val, double qun)
    {
        int index = Findindex(id, code);

        if (index != -1)
        {
            items[index].Quantity += qun;

        }
        else
        {
            Entities.Cart_Item c = new Entities.Cart_Item();
            c.ID = id;
            c.Code = code;
            c.Name = name;
            c.Price = double.Parse(pri.ToString());
            c.Re_price1 = double.Parse(r_price.ToString());
            c.PV = float.Parse(pointv.ToString());
            c.BV = float.Parse(busi_val.ToString());
            c.Quantity = qun;
            items.Add(c);

        }


    }

    #endregion

    #region Total Calculate

    public double total
    {
        get
        {
            double tot = 0;
            foreach (Entities.Cart_Item c in items)
            {
                tot = tot + c.Total_Amount;
            }
            return tot;
        }


    }

    #endregion

    #region Total R_price

    public double total_Rprice
    {
        get
        {
            double tot_r = 0;
            foreach (Entities.Cart_Item c in items)
            {
                Bestlife.ERP.DataAccess.Pricecheckup psb = new DataAccess.Pricecheckup();
                double pricetot=double.Parse(psb.returnPrice(c.Total_RAmount.ToString()));
                tot_r = tot_r + pricetot;
            }
            return tot_r;
        }


    }

    #endregion

    #region Total quantity

    public double total_quantity
    {
        get
        {
            double quan = 0;
            foreach (Entities.Cart_Item c in items)
            {
                quan = quan + c.Quantity;
            }
            return quan;
        }


    }

    #endregion

    #region  Discount

    public double Tot_discounttot
    {
        get
        {
            double discount = 0;
            foreach (Entities.Cart_Item c in items)
            {
                discount =discount+c.Discount;
            }
            return discount;
        }


    }

    #endregion

    #region Total Point_value

    public double totalPoint_value
    {
        get
        {
            double totpv = 0;
            foreach (Entities.Cart_Item c in items)
            {
                totpv = totpv + c.Total_PV;
            }
            return totpv;
        }


    }

    #endregion

    #region Total Business_Value

    public double totalBusiness_Value
    {
        get
        {
            double totbv = 0;
            foreach (Entities.Cart_Item c in items)
            {
                totbv = totbv + c.Total_BV;
            }
            return totbv;
        }


    }

    #endregion

    #region To recipt

    public List<Entities.Return_to_R> reciptvalues
    {
       
        get
        {
            Bestlife.ERP.DataAccess.Pricecheckup chkp = new Bestlife.ERP.DataAccess.Pricecheckup();
            rtnrcptList.Clear();
            foreach (Entities.Cart_Item c in items)
            {
                Entities.Return_to_R rtnrcpt = new Entities.Return_to_R();

                rtnrcpt.Code = c.ID;
                rtnrcpt.Discription = c.Name;
                rtnrcpt.Unit_Price = double.Parse(chkp.returnPrice(c.Re_price1.ToString()));
                rtnrcpt.Quantity = c.Quantity;
                rtnrcpt.PV = decimal.Parse(c.PV.ToString());
                rtnrcpt.BV = decimal.Parse(c.BV.ToString());
                rtnrcpt.Retail_Amount = Math.Round(double.Parse(chkp.returnPrice(c.Total_RAmount.ToString())),2);
                rtnrcpt.Discount = Math.Round(double.Parse(chkp.returnPrice(c.Discount.ToString())),2);
                rtnrcpt.Amount = Math.Round(double.Parse(chkp.returnPrice(c.Total_Amount.ToString())),2);
                rtnrcptList.Add(rtnrcpt);
            }
            return rtnrcptList;
        }


    }

    #endregion

    #region To check to cart

    public List<Entities.Tocheck_To_cart> checkTo_cart
    {
        get
        {
            check_ToList.Clear();
            foreach (Entities.Cart_Item c in items)
            {
                Entities.Tocheck_To_cart chktocart = new Entities.Tocheck_To_cart();
                chktocart.Id = c.ID;
                chktocart.Name = c.Name;
                chktocart.Code = c.Code;
                chktocart.Price = decimal.Parse(c.Price.ToString());
                chktocart.Quantity = double.Parse(c.Quantity.ToString());
                chktocart.Pv = decimal.Parse(c.PV.ToString());
                chktocart.Bv = decimal.Parse(c.BV.ToString());

                check_ToList.Add(chktocart);
            }
            return check_ToList;
        }


    }

    #endregion

    #region Update  Cart

    public void updateItems(string id, string code, string name, double pri, float pointv, float busi_val, int qun)
    {
        int index = Findindex(id, code);

        if (index != -1)
        {
            items[index].Quantity = qun;
            Entities.Cart_Item c = new Entities.Cart_Item();
            c.ID = id;
            c.Code = code;
            c.Name = name;
            c.Price = pri;
            c.PV = pointv;
            c.BV = busi_val;
            c.Quantity = qun;
            items.Add(c);
            items.RemoveAt(index);
        }
    }


    #endregion

    }
}
